<?php
/**
 * Created by PhpStorm.
<<<<<<< HEAD
 * User: wl
 * Date: 2018/6/5
 * Time: 22:22
=======
 * User: xiaotao
 * Date: 2018/6/5
 * Time: 22:49
>>>>>>> a288718df7999637545aa0c9a0ff6d6b164b13dd
 */

namespace app\index\controller;
use think\Db;
use think\Controller;
use think\facade\Env;

class Basic extends Controller
{
    /**
     * 自动执行方法，不需要子类调用
     */
    protected function initialize()
    {
        //获取配置文件
        $indexConfig = include(Env::get('module_path').'/setting.php');
        $data=[['key'=>"top",
            'value'=>json_encode($indexConfig['about']['top'], JSON_UNESCAPED_UNICODE),
            'prefix'=>"about"],
            ['key'=>"mid",
                'value'=>json_encode($indexConfig['about']['mid'], JSON_UNESCAPED_UNICODE),
                'prefix'=>"about"],
            ['key'=>"bottom",
                'value'=>json_encode($indexConfig['about']['bottom'], JSON_UNESCAPED_UNICODE),
                'prefix'=>"about"],
            ['key'=>"page1",
                'value'=>json_encode($indexConfig['index']['page1'], JSON_UNESCAPED_UNICODE),
                'prefix'=>"index"],
            ['key'=>"page2",
                'value'=>json_encode($indexConfig['index']['page2'], JSON_UNESCAPED_UNICODE),
                'prefix'=>"index"],
            ['key'=>"page3",
                'value'=>json_encode($indexConfig['index']['page3'], JSON_UNESCAPED_UNICODE),
                'prefix'=>"index"],
            ['key'=>"page4",
                'value'=>json_encode($indexConfig['index']['page4'], JSON_UNESCAPED_UNICODE),
                'prefix'=>"index"],
            ['key'=>"page5",
                'value'=>json_encode($indexConfig['index']['page5'], JSON_UNESCAPED_UNICODE),
                'prefix'=>"index"],
            ['key'=>"page6",
                'value'=>json_encode($indexConfig['index']['page6'], JSON_UNESCAPED_UNICODE),
                'prefix'=>"index"],];
        //dump($data);
        //插入数据
        //$res=Db::table('config')->data($data)->insertAll();
        //模板赋值输出
        $menuList = $this->getMenu(0);
        $bottomLink= $this->getMenu(2);

        $this->assign([
            'menuList'      => $menuList,
            'bottomLink'    => $bottomLink,
            'contact'       => $this->getConfigs('contact'),
            'banner'        => $this->getConfigs('banner')
        ]);
    }

    /**
     * 获取指定类型的菜单
     * @pa int $type
     * @return mixed
     */
    protected function getMenu($type = 0) {
        $menus = Db::table('menu')
            ->where('status',1)->where('type', $type)
            ->field(['sort', 'type', 'status'], true)
            ->order('sort')
            ->select();
        return $menus;

        // 关于menu中多个附加字段，使用案例
        // link只是最基本的跳转，相应数，站外则不能使用url函数
        // is_blank，则表示了是否在新的标签页打开链接，对应于a标签的blank属性，所以模板中需要做相应的判断
    }


    /**
     * 获取分类的子级分类
     * @param int $group
     * @param int $pid
     * @return mixed|null
     */
    protected function getCategory($group = 0, $pid = 0) {
        $categories = Db::table('category')
            ->where('group', $group)->where('status', 1)
            ->order(['id', 'sort'])
            ->field(['status'], true)
            ->select();
        $res = self::ListTree($categories, $pid);

        if (count($res) > 0)
            return $res[0];
        else
            return null;
    }

    /**
     * 根据文章ID获得一条文章内容数据
     * @param int $article_id
     * @return array|null|\PDOStatement|string|\think\Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    protected function getArticle($article_id = 0)
    {
        $article =Db::name('article')
            ->where('id', $article_id)
            ->find();
        return $article;
    }

    /**
     * 获取指定模块的配置信息，key=>(label,value)
     * @param string $prefix
     * @return array
     */
    protected function getConfigs($prefix = 'index') {
        $list = Db::table('config')
            ->where('prefix', $prefix)->where('status', 1)
            ->field(['prefix', 'status'], true)
            ->select();
        $data = [];
        foreach ($list as $item) {
            $temp = ['label' => $item['label']];
            switch ($item['type']) {
                case '1':
                    $temp['value'] = intval($item['value']);
                    break;
                case '2':
                    $temp['value'] = json_decode($item['value'],true);
                    break;
                default:
                    $temp['value'] = $item['value'];
            }
            $data[$item['key']] = $temp;
        }
        return $data;
    }
    
    /**
     * 实现多级分类
     * @param $list
     * @param int $pid
     * @param string $key
     * @param string $parent_node
     * @param string $child_node
     * @return array
     */
    public static function ListTree($list, $pid = 0, $key = 'id', $parent_node = 'pid', $child_node = 'children') {
        $data = [];
        foreach ($list as $item) {
            if ($item[$parent_node] === $pid) {
                $item[$child_node] = self::ListTree($list, $item[$key], $key, $parent_node, $child_node);
                $data[] = $item;
            }
        }
        return $data;
    }
}
